﻿@model NineSeas.ViewModels.Admin.BooksViewModel
@{    
    Style.Require("NineSeas.Books.Summary");
    Script.Require("jQuery").AtHead();
    Script.Require("ShapesBase");

}
<div class="manage">
    <a class="button primaryAction" href="@Url.Action("Create", "BookAdmin")">@T("Create New Book")</a>
</div>
@using (Html.BeginForm("Index", "BookAdmin", FormMethod.Get))
{
    <fieldset class="bulk-actions">
        <label for="search">@T("Search:")</label>
        @Html.TextBoxFor(m => m.Search.Expression)
        <button type="submit">@T("Search")</button>
        <a href="@Url.Action("Index")">@T("Clear")</a>
    </fieldset>
}
@if (Model.Books != null && Model.Books.Count > 0) {
    <ul class="contentItems">
    @foreach (var book in Model.Books) {
        var imageUrl = String.IsNullOrWhiteSpace(book.ImageUrl) ?
            Href("~/Modules/NineSeas/Images/placeholder-book.png")
            : book.ImageUrl;
        
        var tagsHtml = new List<IHtmlString>();
        foreach (string t in book.Tags)
        {
            if (tagsHtml.Any())
            {
                tagsHtml.Add(new HtmlString(" "));
            }
            tagsHtml.Add(Html.ActionLink(t, "Search", "Home", new { area = "Orchard.Tags", tagName = t }, new { }));
        }
        <li>
            <div class="book-leftPane">
                <img src="@imageUrl" alt="@book.Name" class="book-image"/>
            </div>
            <div class="book-rightPane">
                <div class="book-rightPane-header">                           
                    <div class="book-commands">
                        <a href="@Url.Action("Edit", "BookAdmin", new {book.Id})" title="@T("Edit")">@T("Edit")</a>@T(" | ")
                        <a href="@Url.Action("Chapters", "BookAdmin", new {book.Id})" title="@T("Chapters")">@book.ChapterCount @T("Chapters")</a>@T(" | ")
                        <a href="@Url.Action("Delete", new {book.Id, returnUrl = Request.Url.PathAndQuery})" onclick="return confirm('@T("Are you sure you want to delete?")')">@T("Delete")</a>
                    </div>
                </div>
                <div class="book-rightPane-content">      
                    <div class="book-name">
                        <a href="@Url.Action("Index", "BookView", new { name = book.NameOnUrl })">@book.Name</a>
                        @if (!book.VersionRecord.Published)
                        {
                            var message = book.DraftCount > 0 ? T("Book has drafts - {0} modified chapter(s) are unpublished", book.DraftCount) : T("Book has draft.");
                            @Display(New.Controls_HelpPopup(Message: message, Width: 180))
                        }
                    </div>
                    <div class="book-author" data-id="@book.AuthorId"></div>
                    <div class="book-createdDate">@T("Created") @book.CreatedDate</div>
                    <div class="book-lastUpdatedDate">@T("Last Updated") @book.LastUpdatedDate</div>
                    <div class="book-introduction">
                        @(new MvcHtmlString(book.Introduction))
                    </div>
                    <div class="tags">@foreach (var htmlString in tagsHtml)
                                      { @htmlString }</div>  
                </div>
                <div class="book-rightPane-footer"></div>
            </div>
        </li>
    } 
    </ul>
    <script type="text/javascript">
        $(document).ready(function () {
            var authorIds = [];
            $('.book-author').each(function () {
                if ($(this).attr("data-id") != ''
                    && $(this).attr("data-id") != null
                    && authorIds.indexOf($(this).attr("data-id")) == -1) {
                    authorIds.push($(this).attr("data-id"));
                }
            });

            $.each(authorIds, function (index, item) {
                $.ajax({
                    url: '@Href("~/api/nineseas/authors/")' + item,
                    cache: false,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    complete: function (data) {
                        var author = $.parseJSON(data.responseText);

                        var authorLink = '<a href="@Href("~/")Authors/Author/' + item + '/Details">' + author.DisplayName + '</a>';
                        $('.book-author[data-id=' + item + ']').html(authorLink);
                    }
                });
            });
        });
    </script>
    @Display(Model.Pager)
}